﻿using System;
using System.Collections.Generic;

namespace Blog.Joubin.Ca.CodeGen
{
    public class Program
    {
        static void Main(string[] args)
        {
            if (args == null || args.Length == 0)
            {
                WriteUsage();
            }
            else
            {
                try
                {
                    Blog.Joubin.Ca.CodeGenerator codeGen = new Blog.Joubin.Ca.CodeGenerator();
                    codeGen.Process(ProcessParameters(args));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("An error occured: {0}", ex.Message));
                    Console.WriteLine();
                    WriteUsage();
                }
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }

        private static Dictionary<string, string> ProcessParameters(string[] args)
        {
            Dictionary<string, string> parms = new Dictionary<string, string>();
            foreach (string parm in args)
            {
                string[] pair = parm.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                parms.Add(pair[0].Trim().ToUpper(), pair[1].Trim());
            }
            return parms;
        }

        private static void WriteUsage()
        {
            Console.WriteLine("The following command line arguments are required:");
            Console.WriteLine("\tNamespace");
            Console.WriteLine("\tClassname");
	        Console.WriteLine("\tDesignPattern");
	        Console.WriteLine("\tOutputDir");
            Console.WriteLine("\tLanguage");
            Console.WriteLine();
            Console.WriteLine(@"Example: Namespace=Blog.Joubin.Ca.Providers Classname=Sample DesignPattern=providermodel OutputDir=C:\Temp\providers Language=cs");
        }
    }
}
